//
//  KWT233KSettingModel.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/8/28.
//

#import "KWT233KSettingModel.h"

@implementation KWT233KSettingModel
+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper
{
    return @{@"num":@"bitWidth"};
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"rows" : [KWT233KSettingModel class],
             @"items" : [KWT233KSettingModel class],
             @"selectList":[DevSettingOptionModel class]};
 }

- (void)readDefult:(NSDictionary *)dic
{
    [self.rows enumerateObjectsUsingBlock:^(KWT233KSettingModel * _Nonnull rowModel, NSUInteger idx, BOOL * _Nonnull stop) {
        //时间校准
        if ([rowModel.type isEqualToString:@"date"]) {
            rowModel.content = [NSString stringWithFormat:@"20%02ld/%02ld/%02ld %02ld:%02ld:%02ld",[dic[@"Emu_ctrlTimYear"] integerValue]%100,[dic[@"Emu_ctrlTimMon"] integerValue],[dic[@"Emu_ctrlTimDay"] integerValue],[dic[@"Emu_ctrlTimHur"] integerValue],[dic[@"Emu_ctrlTimMin"] integerValue],[dic[@"Emu_ctrlTimSec"] integerValue]];
            return;
        }
        //充放电时间设置
        if ([rowModel.type isEqualToString:@"time"]) {
            [rowModel.items enumerateObjectsUsingBlock:^(KWT233KSettingModel * _Nonnull itemModel, NSUInteger idx, BOOL * _Nonnull stop) {
                NSLog(@"key:%@   value:%@",itemModel.key,dic[itemModel.key]);
               
                if ([itemModel.type isEqualToString:@"select"]) {
                    //下标值
                    for (DevSettingOptionModel *select_model in itemModel.selectList) {
                        if([dic[itemModel.key] integerValue] == select_model.textValue){
                            itemModel.enumText = select_model.text;
                            itemModel.content = [NSString stringWithFormat:@"%ld",select_model.textValue];
                        }
                    }
                }
                //充放电开始时间和结束时间
                if ([itemModel.type isEqualToString:@"date"]) {
                    //小时
                    NSInteger hour = [dic[itemModel.key] integerValue]/100;
                    //分钟
                    NSInteger minute = [dic[itemModel.key] integerValue]%100;
                    itemModel.content = [NSString stringWithFormat:@"%02ld:%02ld",hour,minute];//
                  
                }
                if ([itemModel.type isEqualToString:@"switch"]){
                    itemModel.content = dic[itemModel.key];
                }
                if ([itemModel.type isEqualToString:@"number"]){
                    itemModel.content = dic[itemModel.key];
                }
   
            }];
            //充放电状态根据功率来判断
            KWT233KSettingModel *stateModel = [rowModel.items firstObject];
            KWT233KSettingModel *powerModel = [rowModel.items safeObjectAtIndex:2];
            if ([powerModel.content doubleValue]>0) {
                stateModel.content = @"1";//充电
            }else if ([powerModel.content doubleValue]<0) {
                stateModel.content = @"2";//放电
            }else{
                stateModel.content = @"0";
            }
            for (DevSettingOptionModel *select_model in stateModel.selectList) {
                if([stateModel.content integerValue] == select_model.textValue){
                    stateModel.enumText = select_model.text;
                }
            }
            return;
        }
        
        
        
        //开关和数值输入
        NSLog(@"----->key:%@   value:%@",rowModel.key,dic[rowModel.key]);
        rowModel.content = ValidNullData(dic[rowModel.key]);
        
        //选择
        if ([rowModel.type isEqualToString:@"select"]) {
            //下标值
            for (DevSettingOptionModel *select_model in rowModel.selectList) {
                if([dic[rowModel.key] integerValue] == select_model.textValue){
                    rowModel.enumText = select_model.text;
                }
            }
        }
    }];
}
@end
